#!/bin/bash

script_dir=$(dirname $(readlink -f $0))

# crontab共6段，第6段为要执行的命令或脚本
# *  *  *  *  *  /path/to/script.sh
# |  |  |  |  |__ 星期 0~7
# |  |  |  |_____ 月份 1~12
# |  |  |________ 日期 1~31
# |  |___________ 小时 0~23
# |______________ 分钟 0~59
# 使用application.yml配置
job_1="0 2 1 * * sh ${script_dir}/start.sh > /dev/null 2>&1"
# 使用application-archive.yml配置
job_2="0 3 1 * * sh ${script_dir}/start.sh -c ${script_dir}/application-archive.yml > /dev/null 2>&1"

delete_job=0
add_job_2=0
while getopts "ad" opt; do
  case $opt in
    a)
      add_job_2=1 ;;
    d)
      delete_job=1 ;;
  esac
done

# 默认禁用job2
add_job_2=0

crontab -l | grep -v ${script_dir}/start.sh > tmp.cron

if [ $delete_job -eq 0 ]; then

  echo "$job_1" >> tmp.cron
  echo "add cleaner job: $job_1"

  if [ $add_job_2 -eq 1 ]; then
      echo "$job_2" >> tmp.cron
      echo "add archive job: $job_2"
  fi

fi

crontab tmp.cron

rm tmp.cron